//
//  SettingViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingFeedbackController.h"
#import "SettingPlusController.h"
#import "SettingChangeController.h"
#import "ProgressManager.h"
@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@end
static NSString *cellMain = @"cellMain";
@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark -
#pragma mark - 项目初始化
-(void)setupUI
{
    UITableView *tableView = [[UITableView alloc] init];
    _tableView = tableView;
    
    [self.view addSubview:tableView];
    //代理
    tableView.dataSource = self;
    tableView.delegate = self;
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    //注册
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellMain];
    
    
    //消除tableView的分割线
    UIView * footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = footView;
    
}

#pragma mark -
#pragma mark - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMain forIndexPath:indexPath];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"反馈意见";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"加号符号说明";
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"修改密码";
    }else{
        cell.textLabel.text = @"注销账号";
    }
    return cell;
}

#pragma mark -
#pragma mark - 点击cell的时候调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //取消选中状态
    [cell setSelected:NO];
    [cell setHighlighted:NO];
/****************************************************/
    
    if (indexPath.row == 0) {
        SettingFeedbackController *feedbackVC = [[UIStoryboard storyboardWithName:@"SettingFeedback" bundle:nil] instantiateViewControllerWithIdentifier:@"feedback"];
        [self.navigationController pushViewController:feedbackVC animated:YES];
    }else if (indexPath.row == 1){
        SettingPlusController *plusVC = [[SettingPlusController alloc] init];
        [self.navigationController pushViewController:plusVC animated:YES];
    }else if (indexPath.row == 2){
        //没登录的时候不能跳转
        if ([MLDefaults boolForKey:kIsUserLoginKey]) {
            SettingChangeController *changeVC =  [[UIStoryboard storyboardWithName:@"SettingChange" bundle:nil] instantiateViewControllerWithIdentifier:@"change"];
            changeVC.isForget = NO;
            [self.navigationController pushViewController:changeVC animated:YES];
        }else{
            [ProgressManager alertErrorStatus:@"请先登录"];
        }
    }else{
        if ([MLDefaults boolForKey:kIsUserLoginKey]) {
            [self setup11];
        }else{
            [ProgressManager alertErrorStatus:@"请先登录"];
        }
    }
    NSLog(@"%zd",indexPath.row);
    
    
    
}

#pragma mark -
#pragma mark - 注销提示框
-(void)setup11
{
    //(UIAlertControllerStyle) 展示的类型
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"你确定要退出登录吗" preferredStyle:UIAlertControllerStyleActionSheet];
    
    //默认只有标题 没有操作的按钮:添加操作的按钮 UIAlertAction
    
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    //添加确定
    UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kIsUserQuitKey object:nil];
        //pop到跟控制器
        //显示左边视图
        JASidePanelController *sideVc = (JASidePanelController*)[UIApplication sharedApplication].keyWindow.rootViewController;
        
        [sideVc showLeftPanelAnimated:YES];
         //[self.navigationController popToRootViewControllerAnimated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    //将action添加到控制器
    [alertVc addAction:cancelBtn];
    [alertVc addAction:sureBtn];
    
    //展示
    [self presentViewController:alertVc animated:YES completion:nil];
}

@end
